define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            var factory_id = Fast.api.query('factory_id');
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'factory/factory/index' + location.search,
                    add_url: 'factory/factory/add',
                    edit_url: 'factory/factory/edit',
                    del_url: 'factory/factory/del',
                    multi_url: 'factory/factory/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'category.title', title: __('分类') },
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'name', title: __('门店联系人'), operate: 'LIKE' },
                        { field: 'county.pid', title: __('城市公司'), searchList:Config.city_lists, formatter: Table.api.formatter.normal },
                        { field: 'company_id', title: __('区县公司'), searchList:Config.county_lists, formatter: Table.api.formatter.normal },
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        { field: 'mobile', title: __('手机号'), operate: 'LIKE' },
                        { field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        { field: 'money', title: __('Money'), operate: false, sortable: true },
                        { field: 'score', title: __('Score'), operate: false, sortable: true },
                        { field: 'idcard_face', title: __('Idcard_face'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'idcard_emblem', title: __('Idcard_emblem'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'business_license', title: __('Business_license'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'contract_file', title: __('Contract_file'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        { field: 'bank_image', title: __('Bank_image'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        {field: 'order.total_amount', title: __('Total_amount'), operate: false},
                        {field: 'order.fee_items', title: __('fee_items'), formatter: function (value, row, index) {
                                if (row.order) {
                                    var html = '';
                                    var fee_items = JSON.parse(row.order.items);
                                    $.each(fee_items, function (i, v) {
                                        html += v.item + ':' + v.amount + '元<br>';
                                    });

                                    return html;
                                }
                            }, operate: false},
                        {field: 'order.deduct_fee', title: __('Deduct_fee'), operate: false},
                        {field: 'order.is_pay', title: __('Is_pay'), searchList: {"0":__('Is_pay 0'),"1":__('Is_pay 1')}, formatter: Table.api.formatter.normal},
                        {field: 'order.paytime', title: __('Paytime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.is_refund', title: __('Is_refund'), searchList: {"0":__('Is_refund 0'),"1":__('Is_refund 1')}, formatter: Table.api.formatter.normal},
                        {field: 'order.refund_amount', title: __('Refund_amount'), operate:false},
                        {field: 'order.refundtime', title: __('Refundtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order.state', title: __('State'), searchList: {"-2": __('State -2'), "-1": __('State -1'), "0": __('State 0'), "1": __('State 1')}, formatter: Table.api.formatter.normal},
                        {field: 'order.statetime', title: __('Statetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        // {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        { field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: { normal: __('Normal'), hidden: __('Hidden') } },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'pass',
                                    text: __('通过'),
                                    title: __('通过'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    url: 'fee_order/state?state=1&order_id={row.order.id}',
                                    confirm: '确认审核通过吗？',
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.order && row.order.state === 0;
                                    }
                                },
                                {
                                    name: 'reject',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: 'fee_order/state?state=-1&order_id={row.order.id}',
                                    confirm: '确认审核拒绝吗？',
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.order && row.order.state === 0;
                                    }
                                },
                                {
                                    name: 'log',
                                    text: __('扣除记录'),
                                    title: __('保证金扣除记录'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: "fee_log/index?order_id={row.order.id}",
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        // console.log(row.order.id);
                                        //返回true时按钮显示,返回false隐藏
                                        return row.order;
                                    }
                                },
                                {
                                    name: 'deduct',
                                    text: __('扣保证金'),
                                    title: __('扣保证金'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: "fee_log/add?order_id={row.order.id}",
                                    callback: function (data) {
                                        // Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return row.order && row.order.is_refund === 0;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        dashboard: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'factory/factory/dashboard',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'total_turnover',
                sortOrder: 'desc',
                search: false,
                showToggle: false,
                showExport: false,
                showColumns: false,
                columns: [
                    [
                        { field: "rank", title: __("Rank"), width: "15%", operate: false },
                        { field: "nickname", title: __("Nickname")},
                        { field: "total_turnover", title: __("Total_turnover"), width: "30%", operate: false },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'commission',
                                    text: __('收益明细'),
                                    title: __('收益明细'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    extend: 'data-area=\'["90%", "90%"]\'',
                                    url: 'user/money_log/index?user_id={ids}',
                                }
                            ],
                            width: "30%",
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // $(document).on("change", "#c-company_id", function (row, obj) {
            //     $('#c-category_id').selectPageClear();
            // });
            // $("#c-category_id").data("params", function (row, obj) {
            //     if ($("#c-company_id").val()!=undefined) {
            //         var admins_id = $("#c-company_id").val();
            //     } else {
            //         var admins_id = $("#c-admin_id").val();
            //     }
            //     return { custom: { admin_id: admins_id } };
            // })
            Controller.api.bindevent();
        },
        edit: function () {

            // $(document).on("change", "#c-company_id", function (row, obj) {
            //     $('#c-category_id').selectPageClear();
            // });

            // $("#c-category_id").data("params", function (obj) {
            //     if ($("#c-company_id").val()!=undefined) {
            //         var admins_id = $("#c-company_id").val();
            //     } else {
            //         var admins_id = $("#c-admin_id").val();
            //     }
            //     return { custom: { admin_id: admins_id } };
            // })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-is_fee", function(){
                    //开关切换后的回调事件
                    if ($(this).val() === '1') {
                        $('.fee').show();
                    } else {
                        $('.fee').hide();
                    }
                });

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});